<?php
defined('DCS_ROOT') or exit(header("HTTP/1.1 430 Not Forbidden"));

class ajax {

	function __construct (){
		$this->url = getUrl();
		//de($this->url);		
		$action = $this->url['params']['1'];
		if($action == 'mainreview'){
			$this->mainReview();
		}elseif($action == 'maincomment'){
			$this->mainComment();
		}elseif($action == 'getcomment'){
			$this->getComment();
		}else{
			$this->defaultAction();
		}
		exit();
	}
	
	function defaultAction(){
		exit();
	}

	function mainComment(){
		$db  = getDB();
		$pagetemp = $this->url['params']['2'];

		$param['table'] = ' movie_comments AS c,member AS m,movie AS m2  ';
		$param['field'] = ' c.id,c.uid,c.mid,c.rating,c.contents,c.rt,c.comments,c.weibo_id,m.username,m.avatars,m.profile_image_url,m2.id,m2.title mtitle  ';
		$param['where'] = "  c.uid = m.uid AND c.mid = m2.id AND contents != '' ";
		$param['order'] = ' c.addtime DESC';
		$param['page'] = is_numeric($pagetemp) ? $pagetemp :'1';		
		$param['rows'] = '4';
		$comments = $db->getList($param);
		if(!empty($comments)){
			foreach($comments as $k=>$v){
				$v['addtime'] = date('m月d日',$v['addtime']);

				echo '<div class="ctsh" style="float:left;margin-bottom:10px;border-bottom:1px #ccc dashed;padding-bottom:10px">
					<div style="float:left;width:55px;"><a href="/people/'.$v['uid'].'/" target="_blank" ><img class="pil" src="'.$v['profile_image_url'].'"/></a></div>
					<div style="float:right;;width:535px;">
					<a href="/people/'.$v['uid'].'/" target="_blank">'.$v['username'].'</a>&nbsp;<span style="color:#999">评论</span>&nbsp;<a href="/movie/'.$v['mid'].'/">'.$v['mtitle'].'</a>：
			<span id="message_'.$v['weibo_id'].'">'.html2Txt($v['contents']).'  </span>
					</div>	
				<div align="right" id="'.$v['weibo_id'].'"><a href="javascript:rt('.$v['weibo_id'].','.$v['mid'].')" value="" title="转发到新浪微博" >转发(<span id="rt_sum'.$v['weibo_id'].'">'.$v['rt'].'</span>)</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a >收藏</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp; <a href="javascript:Comment('.$v['weibo_id'].','.$v['mid'].')" value=""  title="评论到新浪微博" >评论(<span id="re_sum{weibo_id}">'.$v['comments'].'</span>)</a></div>
				</div>
				<div class="clear"></div>
				';
			}
			$prev = $param['page'] - 1;
			$prev = $prev < 1 ? 1 : $prev;
			$next = $param['page'] + 1;


			echo '<p class="pl" align="right">
			<a href="javascript:;" onclick="cPrevNext('.$prev.');" class="prev1" >上一页</a>&nbsp;|&nbsp;
			<a href="javascript:;" onclick="cPrevNext('.$next.');" class="next1">下一页</a></p>';
		}else{
			echo 'empty';
		}
		
	}

	function mainReview(){
		$db  = getDB();
		$pagetemp = $this->url['params']['2'];
		$param['table'] = ' movie_review AS r,member AS m,movie AS m2  ';
		$param['field'] = ' r.id,r.uid,r.mid,r.rating,r.title,r.contents,r.reply,r.addtime,m.username,m.avatars,m.profile_image_url,m2.title as mtitle   ';
		$param['where'] = " r.uid = m.uid AND r.mid = m2.id ";
		$param['order'] = ' r.addtime DESC';
		$param['page'] = is_numeric($pagetemp) ? $pagetemp :'1';		
		$param['rows'] = '3';
		$comments = $db->getList($param);
		if(!empty($comments)){
			foreach($comments as $k=>$v){
				$v['addtime'] = date('m月d日',$v['addtime']);
				$v['contents'] = csubstr(html2Txt($v['contents']),120);
				$v['stars'] = getStarImg($v['rating']);
                 //上面的内容，应该与getreviews函数一直， 下面是输出的内容，内容应该与首页的html一致
				echo '
			<div class="ctsh">
			<ul class="tlst clearfix" style="clear:both">
				<li class="nlst"><h3><a href="/review/'.$v['id'].'/" target="_blank">'.$v['title'].'</a><div class="rr"><a href="/review/'.$v['id'].'/"><img src="'.PUB.'img/arrow2.gif"/></a></div></h3>
				</li>
				<li class="ilst" style="clear:both;"><a href="/people/'.$v['uid'].'/"><img class="pil" src="'.$v['profile_image_url'].'"/></a></li>   
				<li class="clst">
					<span class="pl ll obss"><span class="starb">
					<a href="/people/'.$v['uid'].'/">'.$v['username'].'</a>&nbsp;</span>评论 <a href="/movie/'.$v['mid'].'/">'.$v['mtitle'].'</a>
					<span class="stars'.$v['stars'].'"></span></span><br/><br/>
					<div>'.$v['contents'].'...... 
					<a class="pl" href="/review/'.$v['id'].'/">('.$v['reply'].'回应)</a><br/>
					<div class="pl clearfix"><span class="fleft">'.$v['addtime'].' &nbsp; &nbsp;</span></div></div>
				</li>
			</ul>
			</div>
			<div class="clear"></div>
			';
			}
			$prev = $param['page'] - 1;
			$prev = $prev < 1 ? 1 : $prev;
			$next = $param['page'] + 1;
			echo '<p class="pl" align="right">
			<a href="javascript:;" onclick="rPrevNext('.$prev.');" class="prev1" >上一页</a>&nbsp;|&nbsp;
			<a href="javascript:;" onclick="rPrevNext('.$next.');" class="next1">下一页</a></p>';
		}else{
			echo 'empty';
		}
		
	}

	function getComment(){
		$db  = getDB();
		$mid = $this->url['params']['2'];
		$page = $this->url['params']['3'];
		if(!is_numeric($mid) or !is_numeric($page)){
			exit();
		}
		$nextpage = $page + 1;
		$param['table'] = ' movie_comments AS c,member AS m  ';
		$param['field'] = ' c.id,c.uid,c.contents,m.username,m.avatars,c.profile_image_url ';
		$param['where'] = " c.uid = m.uid AND c.mid = '$mid' AND c.contents != '' ";
		$param['order'] = ' c.addtime DESC';
		$param['page'] = is_numeric($page) ? $page : '1';		
		$param['rows'] = '1';
		$comments = $db->getList($param);		
		if(!empty($comments)){
			$comment = $comments['0'];
			$comment['contents'] = csubstr($comment['contents'],75,1);
			echo '<div class="avatars"><a href="/people/'.$comment['uid'].'/"><img src="'.$comment['profile_image_url'].'"/></a></div><div class="contents"><a href="/people/'.$comment['uid'].'/">'.$comment['username'].'</a>：'.$comment['contents'].'</div>';  //影响首页滚动微博
		}else{
			echo 'empty';
		}		
	}
}

?>
